package com.yootk.config;

import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * Spring监控配置
 */
@Configuration
public class DruidSpringConfig {

    // 定义Druid拦截器
    @Bean("druidStatInterceptor")
    public DruidStatInterceptor getDruidStatInterceptor() {
        DruidStatInterceptor interceptor = new DruidStatInterceptor();
        return interceptor;
    }

    // 定义Druid切入点
    @Bean("druidSpringStatPointcut")
    @Scope("prototype")
    public JdkRegexpMethodPointcut getDruidSpringStatPointcut() {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();   // 方法切片配置
        pointcut.setPatterns("com.yootk.service.*", "com.yootk.action.*", "com.yootk.dao.*");
        return pointcut;
    }

    // AOP处理
    @Bean("druidSpringStatAdvisor")
    public DefaultPointcutAdvisor getDruidSpringStatAdvisor(
            DruidStatInterceptor druidStatInterceptor,          // Druid拦截器
            JdkRegexpMethodPointcut jdkRegexpMethodPointcut     // AOP配置
    ) {
        DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(jdkRegexpMethodPointcut);
        defaultPointcutAdvisor.setAdvice(druidStatInterceptor);
        return defaultPointcutAdvisor;
    }
}
